<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属项目" prop="projectId">
        <el-select v-model="queryParams.projectId" placeholder="请选择所属项目" clearable filterable
          :popper-append-to-body="false" class="product-style">
          <el-option v-for="dict in projectOptions" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属公司" prop="transportCompanyId">
        <el-select v-model="queryParams.transportCompanyId" placeholder="请选择所属公司" clearable filterable>
          <el-option v-for="dict in transportOptions" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="预约状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择预约状态" clearable>
          <el-option v-for="dict in dict.type.booking_state" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="预约时间">
        <el-date-picker v-model="daterangeBookingTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['orderManage:booking:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['orderManage:booking:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['orderManage:booking:remove']">批量删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['orderManage:booking:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bookingList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="预约时间" align="center" prop="bookingTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bookingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属项目" align="center" prop="projectName" show-overflow-tooltip />
      <el-table-column label="所属公司" align="center" prop="transportCompanyName" show-overflow-tooltip />
      <el-table-column label="车型要求" align="center" prop="carTypeClassId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.car_type_id" :value="scope.row.carTypeClassId" />
        </template>
      </el-table-column>
      <el-table-column label="预计车次" align="center" prop="bookingCarNumber" />
      <el-table-column label="所在位置" align="center" prop="location">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleLocation(scope.row)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="资源利用化企业" align="center" prop="disposalLandName" width="120" show-overflow-tooltip />
      <el-table-column label="预约状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.booking_state" :value="scope.row.state" />
        </template>
      </el-table-column>
      <el-table-column label="司机列表" align="center" prop="driverNames" width="100" show-overflow-tooltip />
      <el-table-column label="完成时间" align="center" prop="completeTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.completeTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createdDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleDetail(scope.row)" v-hasPermi="['orderManage:booking:query']">
            详情</el-button>
          <el-button v-if="scope.row.state == '30'" size="mini" type="text" @click="handleComplete(scope.row)"
            v-hasPermi="['orderManage:booking:complete']">
            结束</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.current" :limit.sync="queryParams.size"
      @pagination="getList" />

    <!-- 添加或修改预约对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :inline="true" :rules="rules" label-width="80px">
        <el-form-item label="预约时间" prop="bookingTime">
          <el-date-picker clearable v-model="form.bookingTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择预约时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="所属项目" prop="projectId">
          <el-select v-model="form.projectId" placeholder="请选择所属项目" :popper-append-to-body="false" class="product-style">
            <el-option v-for="dict in projectOptions" :key="dict.id" :label="dict.name" :value="dict.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属公司" prop="transportCompanyId">
          <el-select v-model="form.transportCompanyId" placeholder="请选择所属所属公司">
            <el-option v-for="dict in transportOptions" :key="dict.id" :label="dict.name" :value="dict.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车型要求" prop="carTypeClassId">
          <el-select v-model="form.carTypeClassId" placeholder="请选择车型要求">
            <el-option v-for="dict in dict.type.car_type_id" :key="dict.value" :label="dict.label" :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="预计车次" prop="bookingCarNumber">
          <el-input-number v-model="form.bookingCarNumber" placeholder="请输入预计车次" />
        </el-form-item>
        <el-form-item label="资源利用化企业" prop="disposalLandId">
          <el-select v-model="form.disposalLandId" placeholder="请选择所属资源利用化企业">
            <el-option v-for="dict in disposalLandOptions" :key="dict.id" :label="dict.name" :value="dict.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="完成时间" prop="completeTime">
          <el-date-picker clearable v-model="form.completeTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预约状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择预约状态">
            <el-option v-for="dict in dict.type.booking_state" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="现场图片" prop="images">
          <image-upload v-model="form.images" :limit="5" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--地图显示-->
    <el-dialog :visible.sync="mapOpen" fullscreen append-to-body>
      <TMap :longitude="longitude" :latitude="latitude" :address="address" tid="tmap_booking" />
    </el-dialog>

    <booking-detail :visible.sync="detailOpen" :title="title" :readonly="readonly" :formData="form" />
  </div>
</template>

<script>
import { listBooking, getBooking, delBooking, addBooking, updateBooking, completeBooking } from "@/api/orderManage/booking";
import { allCompany } from "@/api/company/company";
import { allProject } from "@/api/projectManage/project";
import { allDisposalLand } from "@/api/disposal/disposalLand";
import TMap from '@/components/Tmap/TMap';
import TFMap from '@/components/Tmap/TFMap';
import BookingDetail from "@/views/orderManage/booking/bookingDetail";

export default {
  name: "Booking",
  dicts: ['product_type', 'car_type_id', 'booking_state'],
  components: { TMap, TFMap, BookingDetail },
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预约表格数据
      bookingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 预约状态( 10:草稿;20:待分配;30:进行中;40:已完成;50:已过期 )时间范围
      daterangeBookingTime: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        projectId: null,
        bookingTime: null,
        completeTime: null,
        bookingCarNumber: null,
        carType: null,
        carTypeClassId: null,
        location: null,
        disposalLandId: null,
        transportCompanyId: null,
        state: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bookingTime: [
          { required: true, message: "预约时间不能为空", trigger: "blur" }
        ],
      },
      // 清运公司选项
      transportOptions: [],
      // 项目选项
      projectOptions: [],
      disposalLandOptions: [],
      // 是否地图显示弹出层
      mapOpen: false,
      position: {},
      longitude: 118.83414506997173,
      latitude: 31.954718562357378,
      address: '江宁区政府',
      detailOpen: false,
      readonly: true
    };
  },
  created () {
    this.getList();
    this.getTransportCompanyList();
    this.getProjectList();
    this.getDisposalLandList();
  },
  methods: {
    // 清运公司列表
    getTransportCompanyList () {
      allCompany().then(response => {
        this.transportOptions = response.data;
      })
    },
    // 项目列表
    getProjectList () {
      allProject().then(response => {
        this.projectOptions = response.data;
      })
    },
    // 资源利用化企业选项列表
    getDisposalLandList () {
      allDisposalLand().then(response => {
        this.disposalLandOptions = response.data;
      })
    },
    /** 查询预约列表 */
    getList () {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeBookingTime && '' != this.daterangeBookingTime) {
        this.queryParams.params["beginBookingTime"] = this.daterangeBookingTime[0];
        this.queryParams.params["endBookingTime"] = this.daterangeBookingTime[1];
      }
      listBooking(this.queryParams).then(response => {
        this.bookingList = response.records;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        projectId: null,
        bookingTime: null,
        completeTime: null,
        bookingCarNumber: null,
        carType: null,
        carTypeClassId: null,
        location: null,
        disposalLandId: null,
        transportCompanyId: null,
        state: null,
        createdDate: null,
        modifiedDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.daterangeBookingTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加预约";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const id = row.id || this.ids
      getBooking(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预约";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBooking(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBooking(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的数据项？').then(function () {
        return delBooking(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('orderManage/booking/export', {
        ...this.queryParams
      }, `预约单管理_${new Date().getTime()}.xlsx`)
    },
    // 定位
    handleLocation (row) {
      this.longitude = row.location.coordinates[0];
      this.latitude = row.location.coordinates[1];
      this.address = row.address;
      this.mapOpen = true;
    },
    // 详情
    handleDetail (row) {
      this.reset();
      getBooking(row.id).then(response => {
        this.form = response.data;
        this.detailOpen = true;
        this.title = "预约单详情";
        this.readonly = true;
      });
    },
    // 结束
    handleComplete (row) {
      this.$modal.confirm('是否结束该预约单？').then(function () {
        return completeBooking(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => { });
    },
  }
};
</script>

<style scoped>
.product-style /deep/ .el-select-dropdown__item {
  width: 300px;
  display: inline-block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>